package com.myh.gulimall.ware.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.myh.common.constant.WareConstant;
import com.myh.gulimall.ware.dao.PurchaseDao;
import com.myh.gulimall.ware.entity.PurchaseDetailEntity;
import com.myh.gulimall.ware.service.PurchaseDetailService;
import com.myh.gulimall.ware.vo.FinishVo;
import com.myh.gulimall.ware.vo.ItemVo;
import com.myh.gulimall.ware.vo.MergeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.myh.common.utils.PageUtils;
import com.myh.common.utils.Query;

import com.myh.gulimall.ware.entity.PurchaseEntity;
import com.myh.gulimall.ware.service.PurchaseService;
import org.springframework.transaction.annotation.Transactional;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {

    @Autowired
    private PurchaseDao purchaseDao;

    @Autowired
    private PurchaseDetailService purchaseDetailService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                null
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils unreceiveList() {

        QueryWrapper<PurchaseEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1).or().eq("status", 0);
        IPage<PurchaseEntity> iPage = purchaseDao.selectPage(
                new Page<PurchaseEntity>(1, 10),
                queryWrapper
        );
        return new PageUtils(iPage);
    }

    @Override
    @Transactional
    public void merge(MergeVo mergeVo) {
        Long purchaseId = mergeVo.getPurchaseId();
        if (Objects.isNull(purchaseId)) {
            //先进行新增操作
            PurchaseEntity purchaseEntity = new PurchaseEntity();
            purchaseEntity.setCreateTime(new Date());
            purchaseEntity.setUpdateTime(new Date());
            purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.CREATED.getStatusCode());
            this.save(purchaseEntity);
            purchaseId = purchaseEntity.getId();
        }
        // TODO: 2024/2/15 确认采购单状态是0或1
        List<Long> ids = mergeVo.getItems();
        if (!Objects.isNull(ids) && ids.size() != 0){
            Long finalPurchaseId = purchaseId;
            List<PurchaseDetailEntity> collect = ids.stream().map((id) -> {
                PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
                purchaseDetailEntity.setId(id);
                purchaseDetailEntity.setPurchaseId(finalPurchaseId);
                purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.ASSIGNED.getStatusCode());
                return purchaseDetailEntity;
            }).collect(Collectors.toList());
            purchaseDetailService.updateBatchById(collect);
            PurchaseEntity purchaseEntity = new PurchaseEntity();
            purchaseEntity.setId(purchaseId);
            purchaseEntity.setUpdateTime(new Date());
            this.updateById(purchaseEntity);
        }

    }

    @Override
    @Transactional
    public void received(List<Long> ids) {
        //1、确认当前采购单是新建或者已分配状态
        List<PurchaseEntity> collect = ids.stream().filter((id) -> {
            Integer statusById = getStatusById(id);
            if (!Objects.isNull(statusById)) {
                return statusById.equals(WareConstant.PurchaseStatusEnum.CREATED.getStatusCode()) || statusById.equals(WareConstant.PurchaseStatusEnum.ASSIGNED.getStatusCode());
            }
            return false;
        }).map((item) -> {
            PurchaseEntity purchaseEntity = new PurchaseEntity();
            purchaseEntity.setId(item);
            purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.RECEIVE.getStatusCode());
            purchaseEntity.setUpdateTime(new Date());
            return purchaseEntity;
        }).collect(Collectors.toList());
        if (!Objects.isNull(collect) && collect.size() != 0){
            //2、改变采购单的状态
            this.updateBatchById(collect);
            //3、改变采购需求的状态
            purchaseDetailService.updateStatusByPurchaseId(ids);
        }
    }

    // TODO: 2024/2/15 该接口不想写了，以后用到再说吧
    @Override
    public void finish(FinishVo finishVo) {
        //1、改变采购单状态
        Long id = finishVo.getId();
        //2、改变采购需求状态
        List<ItemVo> items = finishVo.getItems();
        items.forEach((item)->{

        });
        //3、将成功采购的进行入库
    }

    public Integer getStatusById(Long id){
        PurchaseEntity purchaseEntity = purchaseDao.selectById(id);
        if (!Objects.isNull(purchaseEntity)){
            return purchaseEntity.getStatus();
        }
        return null;
    }

}